package com.fc.mall.exception;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class ServiceException extends RuntimeException {
    private String errorMessage;

    private Map<String, String> messageVariables;

    public ServiceException(String pattern) {
        super();
        this.errorMessage = pattern;
    }

    @Override
    public String getMessage() {
        return this.errorMessage;
    }

    protected void addMessageVariable(String name, String value) {
        if (messageVariables == null) {
            messageVariables = new HashMap<>();
        }
        messageVariables.put(name, value);
        if (StringUtils.isNotBlank(errorMessage) && errorMessage.indexOf('{') != -1) {
            errorMessage = net.sf.oval.internal.util.StringUtils.replaceAll(errorMessage, "{" + name + "}", value);
        }
    }
}
